package com.wy.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.entity.User;
import com.wy.service.IUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wy.common.R;

import com.wy.service.IInformationService;
import com.wy.entity.Information;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-05-22
 */
@RestController
@RequestMapping("/information")
public class InformationController {

    @Resource
    private IInformationService informationService;
    @Resource
    private IUserService userService;

    /****
     * 删除资讯
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        informationService.removeById(id);
        return R.success("");
    }

    /****
     * 新增 更新资讯
     * @param information
     * @return
     */
    @PostMapping
    public R save(@RequestBody Information information) {
        User user = userService.getById(information.getUserId());
        information.setAuthor(user.getNickname());
        informationService.saveOrUpdate(information);
        return R.success("");
    }

    /****
     * 后台-资讯列表分页 模糊查询标题
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(@RequestParam Integer page,
                      @RequestParam Integer pageSize,
                      @RequestParam String name) {
        LambdaQueryWrapper<Information> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(name)){
            queryWrapper.like(Information::getTitle,name);
        }
        queryWrapper.orderByDesc(Information::getCreateTime);
        return R.success(informationService.page(new Page<>(page, pageSize), queryWrapper));
    }

    /****
     * 获取资讯信息 根据id
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findOne(@PathVariable Integer id) {
        return R.success(informationService.getById(id));
    }

}

